local priority = 4

--Hold info for the left game boundary
local lb = {}
lb.i = love.graphics.newImage('assets/textures/leftBoundary.png')
lb.x = -1080
lb.y = 0

--Hold info for the top game boundary
local tb = {}
tb.i = love.graphics.newImage('assets/textures/topBoundary.png')
tb.x = 0
tb.y = -11000

--Hold info for the top game boundary
local bb = {}
bb.i = love.graphics.newImage('assets/textures/bottomBoundary.png')
bb.x = 0
bb.y = 11000



EntitySystem.rendersystem("drawFG", priority, function()
  local x,y = camera:pos()
  updateLBPos()
  
  camera:detach()
  love.graphics.setColor(color.white)
  love.graphics.draw(lb.i, lb.x - x, lb.y)
  love.graphics.draw(tb.i, tb.x, tb.y - y)
  love.graphics.draw(bb.i, bb.x, bb.y - y)
  camera:attach()
  
  for e in pairs(EntitySystem.query("physical")) do
    if e.position.x <= lb.x + 100 then
      if e.onDeath and e.onDeath.action then
        e.onDeath.needsRun = true
      end
    end
    if e.position.y <= tb.y + 390 then
      if e.onDeath and e.onDeath.action then
        e.onDeath.needsRun = true
      end
    end
    if e.position.y >= bb.y then
      if e.onDeath and e.onDeath.action then
        e.onDeath.needsRun = true
      end
    end
  end
end)

function updateLBPos()
  if player then
    if player.position then
      --Reset position if player at start
      if player.position.x == 0 and player.position.y == 0 then
        lb.x = -1080
        tb.y = -11000
        bb.y = 11000
      end
      
      if ((player.sector.x-1)*1950) > lb.x then
        lb.x = (player.sector.x-2)*1950
      end
    end
    
    if player.health.value ~= 0 then
      lb.x = lb.x + 2
    end
    
  end
end